package com.jy.appmarket.utils;

import java.net.URISyntaxException;

import com.jy.appmarket.R;
import com.jy.appmarket.activity.AppDetailActivity;
import com.jy.appmarket.activity.CardPageActivity;
import com.jy.appmarket.activity.WebViewActivity;
import com.jy.appmarket.model.AppExtras;
import com.jy.appmarket.model.TopicExtras;
import com.jy.appmarket.model.WebExtras;
import com.jy.appmarket.receiver.AppActionReceiver;
import com.jy.appmarket.statistics.ApkSource;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

public class NotificationUtil {

	public static String PUSH_APP = "push_app";

	public static NotificationManager getNotificationManager(Context ctx) {
		return (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public static NotificationCompat.Builder getNotificationBuilder(Context ctx, String title, String detail,
			String icon_url) {
		if (TextUtils.isEmpty(detail)) {
			detail = "";
		}
		if (TextUtils.isEmpty(title)) {
			title = ctx.getString(R.string.app_name);
		}

		NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);

		builder.setSmallIcon(R.drawable.notify_icon).setStyle(new NotificationCompat.BigTextStyle().bigText(detail))
				.setAutoCancel(true).setContentTitle(title).setContentText(detail).setTicker(detail)
				.setPriority(NotificationCompat.PRIORITY_MAX).setDefaults(Notification.DEFAULT_SOUND);
		if (!TextUtils.isEmpty(icon_url)) {
			Bitmap bm = ImageLoader.getInstance().loadImageSync(icon_url);
			if (bm != null) {
				builder.setLargeIcon(bm);
			}
		}
		return builder;
	}

	public static NotificationCompat.Builder getImageNotificationBuilder(Context ctx, String title, String detail,
			Bitmap bm, String icon_url) {
		if (TextUtils.isEmpty(detail)) {
			detail = "";
		}
		if (TextUtils.isEmpty(title)) {
			title = ctx.getString(R.string.app_name);
		}
		NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
		builder.setSmallIcon(R.drawable.notify_icon).setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bm))
				.setAutoCancel(true).setContentTitle(title).setContentText(detail).setTicker(detail)
				.setPriority(NotificationCompat.PRIORITY_MAX).setDefaults(Notification.DEFAULT_SOUND);
		if (!TextUtils.isEmpty(icon_url)) {
			Bitmap icon_bm = ImageLoader.getInstance().loadImageSync(icon_url);
			if (bm != null) {
				builder.setLargeIcon(icon_bm);
			}
		}
		return builder;
	}

	public static void showWebViewNotification(Context ctx, int notificationId, String title, String detail,
			WebExtras extras) {

		NotificationCompat.Builder builder = getNotificationBuilder(ctx, title, detail, extras.getIcon_url());
		Intent intent = new Intent(ctx, WebViewActivity.class);
		intent.putExtra(WebViewActivity.EXTRA_TITLE, title);
		if (extras != null) {
			intent.putExtra(WebViewActivity.EXTRA_URL, extras.getUrl());
			intent.putExtra(WebViewActivity.EXTRA_PUSHID, extras.getPushId());
		}

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		PendingIntent pi = PendingIntent.getActivity(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pi);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	// notification显示图片，点击打开webview
	public static void showImageWebViewNotification(Context ctx, int notificationId, String title, String detail,
			WebExtras extras, Bitmap bitmap) {

		NotificationCompat.Builder builder = getImageNotificationBuilder(ctx, title, detail, bitmap,
				extras.getIcon_url());

		Intent intent = new Intent(ctx, WebViewActivity.class);
		intent.putExtra(WebViewActivity.EXTRA_TITLE, title);
		if (extras != null) {
			intent.putExtra(WebViewActivity.EXTRA_URL, extras.getUrl());
			intent.putExtra(WebViewActivity.EXTRA_PUSHID, extras.getPushId());
		}

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		PendingIntent pi = PendingIntent.getActivity(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pi);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	public static void showImageAppNotification(Context ctx, int notificationId, String title, String detail,
			AppExtras extras, Bitmap bitmap) {
		NotificationCompat.Builder builder = getImageNotificationBuilder(ctx, title, detail, bitmap,
				extras.getIcon_url());

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		Intent intent1 = new Intent(ctx, AppDetailActivity.class);
		intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle b = new Bundle();
		b.putString(AppDetailActivity.EXTRA_NAME, extras.getAppName());
		b.putString(AppDetailActivity.EXTRA_PKG, extras.getPkgName());
		b.putString(AppDetailActivity.EXTRA_SOURCE, new ApkSource(PUSH_APP).toSourceString());
		b.putString(AppDetailActivity.PUSH_ID, extras.getPushId());
		intent1.putExtras(b);
		PendingIntent appdetail = PendingIntent.getActivity(ctx, tag + 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT);

		builder.setContentIntent(appdetail);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	/**
	 * 用于Schema推送
	 */
	public static void showWebSchemaNotification(Context ctx, int notificationId, String title, String detail,
			String webUrl, String pkgName) {
		NotificationCompat.Builder builder = getNotificationBuilder(ctx, title, detail, null);
		try {
			Intent intent = Intent.parseUri(webUrl, Intent.URI_INTENT_SCHEME);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setPackage(pkgName);

			int tag = String.valueOf(System.currentTimeMillis()).hashCode();
			PendingIntent pi = PendingIntent.getActivity(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			builder.setContentIntent(pi);

			getNotificationManager(ctx).notify(notificationId, builder.build());
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

	public static void showTopicNotification(Context ctx, int notificationId, String title, String detail,
			TopicExtras extras) {

		NotificationCompat.Builder builder = getNotificationBuilder(ctx, title, detail, extras.getIcon_url());

		Intent intent = new Intent(ctx, CardPageActivity.class);
		intent.putExtra(CardPageActivity.EXTRA_TITLE, title);
		intent.putExtra(CardPageActivity.EXTRA_PATH, extras.getTopicUrl());
		intent.putExtra(CardPageActivity.PUSH_ID, extras.getPushId());

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		PendingIntent pi = PendingIntent.getActivity(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pi);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	public static void showImageTopicNotification(Context ctx, int notificationId, String title, String detail,
			TopicExtras extras, Bitmap bm) {

		NotificationCompat.Builder builder = getImageNotificationBuilder(ctx, title, detail, bm, extras.getIcon_url());

		Intent intent = new Intent(ctx, CardPageActivity.class);
		intent.putExtra(CardPageActivity.EXTRA_TITLE, title);
		intent.putExtra(CardPageActivity.EXTRA_PATH, extras.getTopicUrl());
		intent.putExtra(CardPageActivity.PUSH_ID, extras.getPushId());

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		PendingIntent pi = PendingIntent.getActivity(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pi);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	public static void showImageOpenAppNotifiation(Context ctx, int notificationId, String title, String detail,
			AppExtras extras, Bitmap bm) {
		NotificationCompat.Builder builder = getImageNotificationBuilder(ctx, title, detail, bm, extras.getIcon_url());

		Intent intent = new Intent(ctx, AppActionReceiver.class);
		intent.setAction(AppActionReceiver.ACTION_OPEN);
		intent.putExtra(AppActionReceiver.APP_EXTRAS, extras);

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		PendingIntent pi = PendingIntent.getBroadcast(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pi);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	public static void showOpenAppNotification(Context ctx, int notificationId, String title, String detail,
			AppExtras extras) {

		NotificationCompat.Builder builder = getNotificationBuilder(ctx, title, detail, extras.getIcon_url());

		Intent intent = new Intent(ctx, AppActionReceiver.class);
		intent.setAction(AppActionReceiver.ACTION_OPEN);
		intent.putExtra(AppActionReceiver.APP_EXTRAS, extras);

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		PendingIntent pi = PendingIntent.getBroadcast(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pi);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	public static void showPushAppNotification(Context ctx, int notificationId, String title, String detail,
			String leftBtntext, String rightBtnText, AppExtras extras, String action) {

		NotificationCompat.Builder builder = getNotificationBuilder(ctx, title, detail, extras.getIcon_url());

		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		Intent intent1 = new Intent(ctx, AppDetailActivity.class);
		intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle b = new Bundle();
		b.putString(AppDetailActivity.EXTRA_NAME, extras.getAppName());
		b.putString(AppDetailActivity.EXTRA_PKG, extras.getPkgName());
		b.putString(AppDetailActivity.PUSH_ID, extras.getPushId());
		b.putString(AppDetailActivity.EXTRA_SOURCE, new ApkSource(PUSH_APP).toSourceString());
		intent1.putExtras(b);
		PendingIntent appdetail = PendingIntent.getActivity(ctx, tag + 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT);

		Intent intent2 = new Intent(ctx, AppActionReceiver.class);
		intent2.setAction(action);
		intent2.putExtra(AppActionReceiver.APP_EXTRAS, extras);
		PendingIntent operation = PendingIntent.getBroadcast(ctx, tag, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

		builder.setContentIntent(appdetail);
		builder.addAction(R.drawable.transparent, leftBtntext, appdetail);
		SpannableStringBuilder ssb = getSpecialString(rightBtnText, 0, rightBtnText.length(), 0XFFFF9500);
		builder.addAction(R.drawable.transparent, ssb, operation);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	public static void showSingleLineNotification(Context ctx, int notificationId, String detail,
			Class<?> activityClass) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
		builder.setSmallIcon(R.drawable.notify_icon).setStyle(new NotificationCompat.BigTextStyle().bigText(detail))
				.setAutoCancel(true).setContentTitle(ctx.getString(R.string.app_name)).setContentText(detail)
				.setTicker(detail).setPriority(NotificationCompat.PRIORITY_MAX).setDefaults(Notification.DEFAULT_SOUND);
		int tag = String.valueOf(System.currentTimeMillis()).hashCode();
		Intent intent = new Intent(ctx, activityClass);
		PendingIntent pi = PendingIntent.getActivity(ctx, tag, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		builder.setContentIntent(pi);

		getNotificationManager(ctx).notify(notificationId, builder.build());
	}

	public static void cancelNotification(Context ctx, int notificationId) {
		getNotificationManager(ctx).cancel(notificationId);
	}

	public static SpannableStringBuilder getSpecialString(String str, int start, int end, int color) {
		SpannableStringBuilder ssb = new SpannableStringBuilder(str);
		ssb.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		return ssb;
	}

}
